﻿layui.config({
    base: "/Scripts/"
}).use(['form', 'layer', 'jquery', 'laypage', 'table', 'laydate'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        $ = layui.jquery,
        table = layui.table,
        laydate = layui.laydate;
    //校区管理列表页面
    var ManageFunction = {
        //加载列表
        showList: function () {
            table.render({
                elem: '#tb_List'
                //, height: 315
              , url: '/SchoolManage/GetSchoolClassInfoList' //数据接口
              , id: 'ListTable'
              , page: true //开启分页
              , method: 'post'
              , cols: [[ //表头    
                //  { field: 'SchoolName', title: '学校名称', width: 100, }
                //,
                { field: 'EnrollmentYear', title: '年级', width: 80, }
                , { field: 'ClassName', title: '班级名称', width: 200, }
                , { field: 'ClassNo', title: '班级编号', width: 100, }
                , { field: 'StudentCount', title: '人数', width: 80, }
                , { field: 'ManagerName', title: '辅导员', width: 80, }
                , { field: 'MajorName', title: '专业方向', width: 100, }
                , { field: 'AddTime', title: '添加时间', templet: '<div>{{ FormatJsonTime(d.AddTime) }}</div>', width: 100, }
                , { field: 'Remark', title: '备注', width: 100, }
                , { toolbar: '#barDemo', title: '操作', width: 150, }
              ]]
            });
            //监听工具条
            table.on('tool(majortool)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('真的要删除么', function (index) {
                        //obj.del();
                        ManageFunction.deleteList(data);
                        layer.close(index);
                    });
                } else if (obj.event === 'edit') {
                    ManageFunction.showAddForm(data);
                }
            });
        },
        //搜索列表信息
        searchList: function () {
            var classNameVal = $('#className');
            //执行重载
            table.reload('ListTable', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
              , where: {
                  className: classNameVal.val()
              }
            });
        },
        //添加修改页面展示
        showAddForm: function (data) {
            var titleName = "添加班级";
            var url = "/SchoolManage/AddClass";
            if (data != null) {
                url = "/SchoolManage/EditClass/" + data.Id;
                titleName = "修改班级";
            }
            var index = layui.layer.open({
                title: titleName,
                type: 2,
                anim: 1,
                resize: true,
                maxmin: true,
                area: ['800px', '400px'],
                content: url,
                success: function (layero, index) {
                }
            })
        },
        //showClassNo: function (data) {
        //    var schoolId = ManageFunction.PrefixInteger(parseInt($("#SchoolId").val()), 2);
        //    var major = ManageFunction.PrefixInteger(parseInt($("#MajorId").val()), 2);
        //    var schoolyear = $("#yearSelect").val();
        //    var EnrollmentYear = $("#EnrollmentYear").val();
        //    if (EnrollmentYear != null && parseInt(EnrollmentYear)) {
        //        schoolyear = EnrollmentYear;
        //    }
        //    else if (data != null && parseInt(data)) {
        //        schoolyear = data;
        //    }
        //    var classNumber = ManageFunction.PrefixInteger(parseInt($("#ClassNumber").val()), 2);
        //    $("#classNo").text(schoolyear + schoolId + major + classNumber);
        //    ManageFunction.showClassName(data);
        //},
        //PrefixInteger: function (num, length) {
        //    return (Array(length).join('0') + num).slice(-length);
        //},
        //showClassName: function (data) {
        //    var majorName = $("#MajorId  option:selected").text();;
        //    var schoolyear = $("#yearSelect").val();
        //    var EnrollmentYear = $("#EnrollmentYear").val();
        //    if (EnrollmentYear != null && parseInt(EnrollmentYear)) {
        //        schoolyear = EnrollmentYear;
        //    }
        //    else if (data != null && parseInt(data)) {
        //        schoolyear = data;
        //    }
        //    var classNumber = ManageFunction.PrefixInteger(parseInt($("#ClassNumber").val()), 2);
        //    $("#className").text(schoolyear + "级" + majorName + "方向" + classNumber + "班级");
        //},
        //删除列表信息
        deleteList: function (data) {
            $.post("/SchoolManage/DeleteClass", { id: data.Id }, function (result) {
                if (result.ResultStatus == 1) {
                    layer.msg(result.Message, function () {
                        window.location.reload();
                    });
                }
                else {
                    layer.msg(result.Message, { icon: 6 });
                }
            });
        },
        //操作返回结果
        resultEditForm: function (FormName, URL) {
            var index = top.layer.msg('数据提交中，请稍候', { icon: 16, time: false, shade: 0.8 });
            $("#" + FormName).ajaxSubmit({
                url: URL,　　　　　　//form提交数据的地址
                type: "post",　　　  //form提交的方式(method:post/get)
                //target:target,　　//服务器返回的响应数据显示在元素(Id)号确定
                // beforeSubmit:function(){},　　//提交前执行的回调函数
                success: function (dataRes) {
                    if (dataRes.ResultStatus == 1) {
                        top.layer.close(index);
                        top.layer.msg(dataRes.Message);
                        layer.closeAll("iframe");
                        parent.location.reload();
                    }
                    else {
                        top.layer.close(index);
                        top.layer.msg(dataRes.Message);
                    }
                },//提交成功后执行的回调函数
                dataType: "json",　　　　　　　//服务器返回数据类型
                clearForm: true,　　　　　　 //提交成功后是否清空表单中的字段值
                restForm: true,　　　　　　  //提交成功后是否重置表单中的字段值，即恢复到页面加载时的状态
                timeout: 6000 　　　　　 　 //设置请求时间，超过该时间后，自动退出请求，单位(毫秒)。
            });
        },
    };
    //加载页面数据
    ManageFunction.showList();
    //查询
    $(".search_btn").click(function () {
        ManageFunction.searchList();
    })

    //添加展示页面
    $(".usersAdd_btn").click(function () {
        ManageFunction.showAddForm();
    })


    //添加列表信息
    form.on("submit(addMsg)", function (data) {
        var url = "/SchoolManage/AddClass";
        if (parseInt($("#Id").val()) > 0) {
            url = "/SchoolManage/EditClass";
        }
        ManageFunction.resultEditForm("addForm", url)
        return false;
    })

    //返回列表，刷新父页面
    $("body").on("click", ".backlist", function () {
        parent.location.reload();
    })

    //年选择器
    var schoolyear = new Date().getFullYear();
    var EnrollmentYear = $("#EnrollmentYear").val();
    if (EnrollmentYear != null && parseInt(EnrollmentYear)) {
        schoolyear = EnrollmentYear;
    }
    laydate.render({
        elem: '#yearSelect'
      , type: 'year'
      , value: schoolyear
      , done: function (value, date) {
          CommonFunction.showClassNo(value);
      }
    });
    CommonFunction.showClassNo(schoolyear);
    //监听学生下拉框
    form.on("select(major)", function (data) {
        CommonFunction.showClassNo();
    });
    form.on("select(classNumber)", function (data) {
        CommonFunction.showClassNo();
    });
    form.on("select(yearSelect)", function (data) {
        CommonFunction.showClassNo();
    });
})
var jQuery;
layui.use(['jquery'], function () {
    $ = layui.jquery;
    jQuery = $;
});
var CommonFunction = {
    showClassNo: function (data) {
        var schoolId = CommonFunction.PrefixInteger(parseInt($("#SchoolId").val()), 2);
        var major = CommonFunction.PrefixInteger(parseInt($("#MajorId").val()), 2);
        var schoolyear = $("#yearSelect").val();
        var EnrollmentYear = $("#EnrollmentYear").val();
        if (EnrollmentYear != null && parseInt(EnrollmentYear)) {
            schoolyear = EnrollmentYear;
        }
        if (data != null && parseInt(data)) {
            schoolyear = data;
        }
        var classNumber = CommonFunction.PrefixInteger(parseInt($("#ClassNumber").val()), 2);
        $("#classNo").text(schoolyear + schoolId + major + classNumber);
        CommonFunction.showClassName(data);
    },
    PrefixInteger: function (num, length) {
        return (Array(length).join('0') + num).slice(-length);
    },
    showClassName: function (data) {
        var majorName = $("#MajorId  option:selected").text();;
        var schoolyear = $("#yearSelect").val();
        var EnrollmentYear = $("#EnrollmentYear").val();
        if (EnrollmentYear != null && parseInt(EnrollmentYear)) {
            schoolyear = EnrollmentYear;
        }
        if (data != null && parseInt(data)) {
            schoolyear = data;
        }
        var classNumber = CommonFunction.PrefixInteger(parseInt($("#ClassNumber").val()), 2);
        $("#className").text(schoolyear + "级" + majorName + "方向" + classNumber + "班级");
    }
}





